$(function(){
	ReceiptApp.init();
});
var ReceiptApp = function(){
	return{
		init:function(){
			ReceiptApp.loadGrid();
		},
		loadGrid:function(){
			gui.widgets.BootStrap.loadGrid({
				gridSelector:"#grid-table",
				pagerSelector:"#grid-pager",
				postData:{
					"receiptNo":function(){
						return $("#receiptNo").val();
					},
					"status":function(){
						return $("#status").val();
					}
				},
				jsonReader : {  
					root: "rows",
					records: "total",  
			        repeatitems : false,
			        total: function (obj) {
                        return  Math.ceil(obj.total /jQuery("#grid-table").getGridParam("rowNum"));
                    },
				} , 
				datatype: 'json',
				url:$('#APP_ROOT_PATH').val() + 'receipt/page',
				colNames:['单号','卖家','付款户名','开户行','卡号','付款时间','金额','类型','申请状态','操作'],
				colModel:[
					{name:'receiptNo',index:'receiptNo',sortable:false,width:100},
					{name:'shopName',index:'shopName',sortable:false,width:100},
					{name:'buyyerName',index:'buyyerName',sortable:false,width:80},
					{name:'bankName',index:'bankName',sortable:false,width:100},
					{name:'cardNo',index:'cardNo',sortable:false,width:80},
					{name:'gmtPay',index:'gmtPay',sortable:false,width:120},
					{name:'amount',index:'gmtCreate',sortable:false,width:80},
					{name:'type',index:'type',sortable:false,width:80,formatter:function(cellvalue, options, rowObject){
						if(cellvalue == '0'){
							return '服务费';
						}
						
						return '';
					}},
					{name:'status',index:'status',sortable:false,width:80,formatter:function(cellvalue, options, rowObject){
						if(cellvalue=='0'){
							return "待确认";
						}
						if(cellvalue=='1'){
							return "收款成功";
						}
					}},
					{name:'status',index:'status',sortable:false,formatter:function(cellvalue, options, rowObject){
						if(cellvalue=='0'){
							return '<a href="javascript:void(0)" onclick="ReceiptApp.receive(' + rowObject.id + ')">确认收款</a>';
						}else{
							return '';
						}
					}}
					
				],
				shrinkToFit:false,
				autoScroll: true,
				caption: "代收款记录",
				rowList:[10,20,30],
				multiselect: true,
				prmNames:{
					page:"pageNum",
					rows:"pageSize", 
					sort: "sortField",
					order: "sortOrder", 
					search:"_search" 
				}
			});
		},
		search:function(){
			ReceiptApp.reload();
		},
		reset:function(){
			$("#receiptNo").val('');
			$("#status").val('');
		},
		reload:function(){
			jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");  
		},
		receive:function(id){
			if(confirm("是否确认已收到线下转账")) {
				var url = $("#APP_ROOT_PATH").val() + '/receipt/receive';
				gui.util.Ajax.asyncGET(url,{id:id},function(res){
					if(res.ret != undefined && res.ret != 0){
						$.gritter.add({
							title: '错误提示',
							text: res.message,
							class_name: 'gritter-error'
						});
					}
					
					window.location.reload()
//					ReceiptApp.reload();
				});
			}
		}
	}
}();